package com.chidopi.member.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.chidopi.member.dao.IMemberMaintainDao;
import com.chidopi.member.domain.BookCities;
import com.chidopi.member.domain.Member2City;
import com.chidopi.member.domain.Member2Subscribe;

public class MemberMaintainDaoImpl implements IMemberMaintainDao {
	private static Logger logger = LoggerFactory.getLogger(MemberMaintainDaoImpl.class);
	
	protected SimpleJdbcTemplate simpleJdbcTemplate;
	@Autowired
	public void init(DataSource datasource) {
		this.simpleJdbcTemplate = new SimpleJdbcTemplate(datasource);
		logger.debug("init: after new JdbcTemplate");
	}
	
	@Override
	public BookCities findBookCitiesByRoleId(int role_id) {
		String sql = "select a.*, b.* " +
				"from bookcities a, bookcityext b where a.id=b.city_id and b.role_id=? and a.enable_flag='Y'";
		RowMapper<BookCities> rm = 
			ParameterizedBeanPropertyRowMapper.newInstance(BookCities.class);
		List<BookCities> list = this.simpleJdbcTemplate.query(sql, rm, role_id);
		if (list == null || list.size()==0) return null;
		return list.get(0);
	}

	@Override
	public List<Member2City> findMember2City(int city_id) {
		String sql = "select * from member2city where city_id=?";
		RowMapper<Member2City> rm = 
			ParameterizedBeanPropertyRowMapper.newInstance(Member2City.class);
		return this.simpleJdbcTemplate.query(sql, rm,  city_id);
	}
}
